# 计算脚本目录与备份目录
$SCRIPT_DIR = $PSScriptRoot
if (-not $SCRIPT_DIR) {
    $SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Definition
}

$BACKUP_DIR = Join-Path $SCRIPT_DIR "..\backups"
New-Item -ItemType Directory -Force -Path $BACKUP_DIR | Out-Null

# 项目名 & 卷名
$PROJECT_NAME = Split-Path -Leaf (Join-Path $SCRIPT_DIR "..")
$MYSQL_VOL    = "${PROJECT_NAME}_mysql_data"
$REDIS_VOL    = "${PROJECT_NAME}_redis_data"

# 导出函数
function Export-Volume {
    param($VolumeName, $FileName)
    Write-Host "=> 导出 $VolumeName ..." -ForegroundColor Green
    docker run --rm `
      -v "${VolumeName}:/vol" `
      -v "${BACKUP_DIR}:/backup" `
      alpine tar czf "/backup/${FileName}" -C /vol .
}

# 执行导出
Export-Volume $MYSQL_VOL "${MYSQL_VOL}.tar.gz"
Export-Volume $REDIS_VOL "${REDIS_VOL}.tar.gz"

# 列出结果
Get-ChildItem (Join-Path $BACKUP_DIR "*.tar.gz") |
    Select-Object Name, @{Name="Size(MB)";Expression={[math]::Round($_.Length/1MB,2)}}